package com.Telien.app.database.connector;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.Telien.app.utils.TelienConfig;

public class JDBCConnector {
	private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
	private static final String DB_URL = "jdbc:mysql://localhost/EMP";
	private static final String dbUserProp = "db.username";
	private static final String dbPasswordProp = "db.password";
	private static final String dbUrlProp = "db.url";
	
	private Connection conn = null;
	
	private TelienConfig config;
	private String dbUser;
	private String dbPassword;
	private String dbURL;
	
	
	public JDBCConnector(){
		config = new TelienConfig();
		dbUser = config.getProperty(dbUserProp);
		dbPassword = config.getProperty(dbPasswordProp);
		dbURL = config.getProperty(dbUrlProp);
	}
	
	public void connect() throws ClassNotFoundException, SQLException{
		Class.forName("com.mysql.jdbc.Driver");
		System.out.println("Connecting to database...");
	    conn = DriverManager.getConnection(dbURL,dbUser,dbPassword);
	}
	
	public ResultSet executeQuery(String query) throws SQLException{
	      System.out.println("Creating statement...");
	      Statement stmt = conn.createStatement();
	      ResultSet rs = stmt.executeQuery(query);
	      return rs;
	}
	
	public void insertRecord(String record) throws SQLException{
		Statement stmt = conn.createStatement();
		 stmt.executeUpdate(record);
	}
	
}
